package com.mindskip.xzs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.dto.student.message.MessagePageStudentDTO;
import com.mindskip.xzs.domain.entity.Message;
import com.mindskip.xzs.domain.entity.MessageUser;
import com.mindskip.xzs.domain.entity.User;
import com.mindskip.xzs.domain.vo.student.message.MessagePageStudentVO;
import com.mindskip.xzs.repository.BaseMapper;
import com.mindskip.xzs.repository.MessageMapper;
import com.mindskip.xzs.repository.MessageUserMapper;
import com.mindskip.xzs.service.MessageUserService;
import com.mindskip.xzs.utility.BeanCopyUtils;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.PageInfoUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class MessageUserServiceImpl extends BaseServiceImpl<MessageUser> implements MessageUserService {

    private final MessageUserMapper messageUserMapper;

    private final MessageMapper messageMapper;

    public MessageUserServiceImpl(BaseMapper<MessageUser> baseMapper, MessageUserMapper messageUserMapper, MessageMapper messageMapper) {
        super(baseMapper);
        this.messageUserMapper = messageUserMapper;
        this.messageMapper = messageMapper;
    }


    @Override
    public List<MessageUser> getMessageUserByMessageIds(List<Integer> ids) {
        return messageUserMapper.selectByMessageIds(ids);
    }

    @Override
    public PageInfo<MessagePageStudentVO> getMessageUserByPageAndReceiveUserId(MessagePageStudentDTO messagePageStudentDTO) {
        PageInfo<MessageUser> oldPageInfo = PageHelper.startPage(messagePageStudentDTO.getPageIndex(), messagePageStudentDTO.getPageSize(), "id desc")
                .doSelectPageInfo(() -> messageUserMapper.selectByReceiveUserId(messagePageStudentDTO.getReceiveUserId()));

        List<Integer> messageIds = oldPageInfo.getList().stream()
                .map(MessageUser::getMessageId)
                .collect(Collectors.toList());
        List<Message> messages = messageMapper.selectByIds(messageIds);

        List<MessagePageStudentVO> messagePageStudentVOS = oldPageInfo.getList().stream()
                .map(messageUser -> {
                    MessagePageStudentVO messagePageStudentVO = BeanCopyUtils.copyBean(messageUser, MessagePageStudentVO.class);

                    Message message = messages.stream()
                                .filter(message1 -> message1.getId().equals(messageUser.getMessageId()))
                                .findFirst().orElse(null);
                    if (ObjectUtils.isEmpty(message)) {
                        throw new RuntimeException("用户消息没有找到对应的消息内容");
                    }

                    messagePageStudentVO.setTitle(message.getTitle());
                    messagePageStudentVO.setContent(message.getContent());
                    messagePageStudentVO.setCreateTime(DateTimeUtil.dateFormat(message.getCreateTime()));
                    messagePageStudentVO.setSendUserName(message.getSendUserName());
                    return messagePageStudentVO;
                })
                .collect(Collectors.toList());

        return PageInfoUtil.copyMap(oldPageInfo, messagePageStudentVOS);
    }

    @Override
    @Transactional
    public void readMessage(Integer id) {

        MessageUser messageUser = messageUserMapper.selectByPrimaryKey(id);
        messageUser.setReaded(true);
        messageUser.setReadTime(new Date());
        System.out.println(messageUser);
        int i = messageUserMapper.updateByPrimaryKeySelective(messageUser);
        if (i <= 0) {
            throw new RuntimeException("消息标记已读失败");
        }

        int i1 = messageUserMapper.updateReadCount(messageUser.getMessageId());
        if (i1 <= 0) {
            throw new RuntimeException("增加消息的已读数量失败");
        }

    }

    @Override
    public Integer getUnreadCount(User currentUser) {

        List<MessageUser> messageUserList = messageUserMapper.selectByReceiveUserId(currentUser.getId());

        Integer unreadCount = 0;
        for (MessageUser messageUser: messageUserList) {
            if (!messageUser.getReaded()) {
                unreadCount++;
            }
        }

        return unreadCount;
    }
}
